package cqie.frame.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cqie.frame.entity.Course;
import cqie.frame.entity.EvaluateType;
import cqie.frame.entity.qo.CourseQo;
import cqie.frame.entity.qo.EvaluateTypeQo;
import cqie.frame.entity.vo.ClazzVo;
import cqie.frame.entity.vo.CourseVo;
import cqie.frame.response.CodeMsg;
import cqie.frame.response.ResultJson;
import cqie.frame.service.IEvaluateTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 评价类型(EvaluateType)表控制层
 *
 * @author makejava
 * @since 2021-10-08 09:06:25
 */
@RestController
@RequestMapping("/evaluateType")
public class EvaluateTypeController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IEvaluateTypeService iEvaluateTypeService;

    //新增新增评价类型
    @ApiOperation(value = "新增评价类型", notes = "新增评价类型，同时更新评价类型表。", response = EvaluateType.class)
    @PostMapping(value = "/addEvaluateType")
    public ResultJson addEvaluateType(@RequestBody EvaluateType po) {
        try {
            //构造mo
            EvaluateType evaluateType = new EvaluateType();
            //复制po的属性到evaluateType,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, evaluateType);
            //插入用户表后回显插入课程的主键id
            iEvaluateTypeService.save(evaluateType);
            po.setId(evaluateType.getId());
            //返回评价类型
            return ResultJson.success(po);
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除选中评价类型，多选
    @ApiOperation(value = "删除选中评价类型", notes = "删除选中评价类型。")
    @PostMapping(value = "/deleteCheckEvaluateType")
    public ResultJson deleteCheckEvaluateType(@RequestBody Long[] evaluateTypeIdList) {
        try {
            for (int i = 0; i < evaluateTypeIdList.length; i++) {
                iEvaluateTypeService.removeById(evaluateTypeIdList[i]);
            }
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除评价类型
    @ApiOperation(value = "删除评价类型", notes = "删除评价类型。")
    @GetMapping(value = "/deleteEvaluateType")
    public ResultJson deleteEvaluateType(long id) {
        try {
            //删除评价类型
            iEvaluateTypeService.removeById(id);
            //返回前端
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }


    //修改评价类型表
    @ApiOperation(value = "修改评价类型表", notes = "修改评价类型表，同时更新评价类型表。")
    @PostMapping(value = "/updateEvaluateType")
    public ResultJson updateEvaluateType(@RequestBody EvaluateType po) {
        try {
            //获取修改前的值
            EvaluateType evaluateType = new EvaluateType();
            //复制po的属性到sysUser,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, evaluateType);
            //修改评价类型
            iEvaluateTypeService.updateById(evaluateType);
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "分页列表", response = EvaluateType.class)
    @PostMapping(value = "/listPaged")
    public ResultJson listPaged(@RequestBody EvaluateTypeQo qo) {
        try {
            Page<EvaluateType> page = new Page<>(qo.getPageIndex(), qo.getPageSize());
            //1.查询条件
            QueryWrapper<EvaluateType> qw = new QueryWrapper<>();
            if (!StringUtils.isEmpty(qo.getName())) {
                //查询模糊查询
                qw.like("name", qo.getName());
            }
            //2.执行查询
            IPage<EvaluateType> datas = iEvaluateTypeService.page(page, qw);
            return ResultJson.success(datas);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "查看详情", response = EvaluateType.class)
    @GetMapping(value = "/details")
    public ResultJson details(Long id) {
        try {
            //1.获取mo
            EvaluateType mo = iEvaluateTypeService.getById(id);

            //3.返回
            return ResultJson.success(mo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }
}

